From 6cb399e7267ae78e3e498bdbf5f51678ffb2cd45 Mon Sep 17 00:00:00 2001 From: ivarlovlie Date: Tue, 21 Feb 2023 23:24:14 +0100 Subject: feat: Many things Configure sanity in same project as the app Implement type safe sanity schema Read localised documents Strip down design --- src/routes/[lang=lang]/+page.server.ts | 39 ++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 11 deletions(-) (limited to 'src/routes/[lang=lang]/+page.server.ts') diff --git a/src/routes/[lang=lang]/+page.server.ts b/src/routes/[lang=lang]/+page.server.ts index c2284ee..deafae3 100644 --- a/src/routes/[lang=lang]/+page.server.ts +++ b/src/routes/[lang=lang]/+page.server.ts @@ -1,22 +1,31 @@ -import { sanity } from "$lib/sanity-client"; +import { sanity } from "$lib/sanity/client"; import type { PageServerLoad } from "./$types"; import groq from "groq"; import type { ContactModel } from "./sections/contact.svelte"; -import { fromLocalizedString } from "$lib/utils"; import type { HeroModel } from "./sections/hero.svelte"; import type { DescriptionModel } from "./sections/description.svelte"; +import type { s } from "sanity-typed-schema-builder"; +import type contactType from "$lib/sanity/schemas/default/contact"; +import type heroType from "$lib/sanity/schemas/default/hero"; +import type descriptionType from "$lib/sanity/schemas/default/description"; +import type productType from "$lib/sanity/schemas/default/product"; +import type { ProductsModel } from "./sections/products.svelte"; export const load = (async ({ locals }) => { - const contactSection = await sanity.fetch(groq`*[_type == "contact"][0]`); - const heroSection = await sanity.fetch(groq`*[_type == "hero"][0]`); - const descriptionSection = await sanity.fetch(groq`*[_type == "description"][0]`); - const products = await sanity.fetch(groq`*[_type == "product"]`); + const commonParams = { + lang: locals.locale + } + const contactSection: s.infer = await sanity.fetch(groq`*[_type == "contact" && __i18n_lang == $lang][0]`, { ...commonParams }) ?? {}; + const heroSection: s.infer = await sanity.fetch(groq`*[_type == "hero" && __i18n_lang == $lang][0]`, { ...commonParams }) ?? {}; + const descriptionSection: s.infer = await sanity.fetch(groq`*[_type == "description" && __i18n_lang == $lang][0]`, { ...commonParams }) ?? {}; + const products: Array> = await sanity.fetch(groq`*[_type == "product" && __i18n_lang == $lang]`, { ...commonParams }) ?? []; + return { contact: { - phone: fromLocalizedString(contactSection.phone, locals.locale), - email: fromLocalizedString(contactSection.email, locals.locale), - phoneHours: fromLocalizedString(contactSection.phoneHours, locals.locale), - addressLines: contactSection.addressLines.map((el: string | object) => fromLocalizedString(el, locals.locale)), + phone: contactSection.phone, + email: contactSection.email, + phoneHours: contactSection.phoneHours, + addressLines: contactSection.addressLines?.map((el: string | object) => el) ?? [], } as ContactModel, hero: { title: heroSection.title, @@ -26,6 +35,14 @@ export const load = (async ({ locals }) => { title: descriptionSection.title, content: descriptionSection.content, } as DescriptionModel, - products: products + products: { + products: products.map((p) => ({ + cost: p.cost, + description: p.description, + duration: p.duration, + orderLink: p.orderLink, + title: p.title + })) + } as ProductsModel }; }) satisfies PageServerLoad; -- cgit v1.3